
var app = getApp();

Page({
    data: {
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 3000,
        duration: 1200,
        iscollect: true
    },

    onLoad:function(options){
        //var book = options.book;
        var book = JSON.parse(options.book);
        //console.log(book);
        var bookid = book.bookid;
        var cartAmount = options.cartAmount;
        this.setData({
            book:book,
            bookid:bookid,
            cartAmount:cartAmount
        })
        this.getBookImages();
    },

    onReachBottom: function() {
      this.getCartBook();  // 下拉刷新
    },

    // ---------------------功能模块 start-------------------------
    getBookImages:function(){
        var mark = 'getBookImages';
        var bookid =this.data.bookid;
        wx.request({
          url: 'http://localhost:8080/youYanZaiXian/MallServlet',
          method: 'POST',
          data:{
            mark:mark,
            bookid:bookid,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          success: (res)=>{
            var bImaList = res.data.data.bImaList;
            this.setData({
              booKImageList:bImaList,
            })
            //console.log(this.data.booKImageList)
          }
        });
      },
    
      // 购物车商品的添加
      addCart:function(e){
        var mark = 'addCart';
        var bookid =  e.currentTarget.dataset.bookid;
        var userid = getApp().globalData.userid;
        //console.log("bookid:"+bookid)
        //console.log("userid:"+userid)
        wx.request({
          url: 'http://localhost:8080/youYanZaiXian/MallServlet',
          method: 'POST',
          data: {
            mark: mark,
            bookid:bookid,
            userid:userid,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          success: (res) => {
            var suc = res.data.data.suc;
            //console.log("是否添加成功："+suc);
            if(suc==1){
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
              });
              this.getCartBook();
            }else{
              wx.showToast({
                title: '添加失败',
                icon: 'error',
                duration: 2000
              })
            }
            
          }
        });
      },

      getCartBook:function(){
        var userid =  getApp().globalData.userid;
        console.log("userid:"+userid);
        var mark = 'getCartBook';
        wx.request({
          url: 'http://localhost:8080/youYanZaiXian/MallServlet',
          method: 'POST',
          data:{
            mark:mark,
            userid:userid,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          success: (res)=>{
            var cartList = res.data.data.cartList;
            
            var cartAmount = cartList.length;
            //console.log("cartAmount:"+cartAmount);
            this.setData({
              cartAmount:cartAmount,
            });
          }
        });
      },

    collect: function(){
        this.setData({
            iscollect: !this.data.iscollect
        })
        console.log(this.data.iscollect);
    },

    // ---------------------功能模块 end-------------------------

    // 前往购物车
    goCart:function(){
      wx.navigateTo({
        url: '/pages/my/shopping_cart/shopping_cart',
      })
    },
    goToPay:function(){
      var book = this.data.book;
      var cartList = [];
      cartList[0] = book;
      var cartListJson = JSON.stringify(cartList);
      var sumPrice = this.data.book.bookPrice;

    wx.navigateTo({
      url: '../pay/pay?sumPrice='+sumPrice+'&cartList='+cartListJson,
    })
    }
})